package com.bruce.phoneFlash;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class ZanActivity extends AppCompatActivity {

    private ViewPager zanPager;
    private ImageView wei;
    private ImageView zan;
    private ImageView zhi;

    private List<View> views;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zan);
        initView();
        initEvent();
    }

    private void initEvent() {
        zanPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                Log.i("zan", "onPageSelected: " + position);
                setNo();
                switch (position) {
                    case 0:
                        zhi.setImageResource(R.drawable.pager_checked);
                        break;
                    case 1:
                        zan.setImageResource(R.drawable.pager_checked);
                        break;
                    case 2:
                        wei.setImageResource(R.drawable.pager_checked);
                        break;
                }
            }

            private void setNo() {
                Log.i("zan", "set no");
                zan.setImageResource(R.drawable.pager_no);
                wei.setImageResource(R.drawable.pager_no);
                zhi.setImageResource(R.drawable.pager_no);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        View zan = getLayoutInflater().inflate(R.layout.zan_layout, null);
        View weiXin = getLayoutInflater().inflate(R.layout.weixin_layout, null);
        View zhi_fu_bo = getLayoutInflater().inflate(R.layout.zhifubao_layout, null);

        zanPager = (ViewPager) findViewById(R.id.zan_pager);
        wei = (ImageView) findViewById(R.id.wei);
        zhi = (ImageView) findViewById(R.id.zhi);
        this.zan = (ImageView) findViewById(R.id.zan);

        views = new ArrayList<>();
        views.add(zhi_fu_bo);
        views.add(zan);
        views.add(weiXin);
        PagerAdapter adapter = new Pager();
        zanPager.setAdapter(adapter);
        zanPager.setCurrentItem(1);
    }

    private class Pager extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));

            return views.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }
    }
}
